{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
{$WARN UNSAFE_TYPE OFF}

unit ProcessController;

interface

  uses Interface_TLB, ComObj, AbstractController;

  type
    TControllerType = (cDownload, cMergeGrf);
    TProcessController = class(TAutoIntfObject, IControllerIntf, IDispatch)
      private
        fController : TController;
      protected
        procedure Toggle; safecall;
        procedure Cancel; safecall;
        function Get_Max: Cardinal; safecall;
        function Get_Current: Cardinal; safecall;
        function Get_Finished: WordBool; safecall;
        function Get_Error: WordBool; safecall;
        function Get_Running: WordBool; safecall;
        property Max: Cardinal read Get_Max;
        property Current: Cardinal read Get_Current;
        property Finished: WordBool read Get_Finished;
        property Error: WordBool read Get_Error;
        property Running: WordBool read Get_Running;
      public
        constructor Create(Path : Widestring; ControllerType : TControllerType);
        destructor Destroy(); override;
    end;

implementation

  uses
    ActiveX, DownloadController{, MergeController};

  constructor TProcessController.Create(Path : Widestring; ControllerType : TControllerType);
  var
    TypeLib : ITypeLib;
    ExeName : Widestring;
  begin
    //Get name of application
    ExeName := ParamStr(0);
    //Load type library from application's resources
    OleCheck(LoadTypeLib(PWideChar(ExeName), TypeLib));
    //Inherit
    inherited Create(TypeLib, IControllerIntf);
    //Create controller
    if ControllerType = cDownload then
      fController := TDownloadController.Create(Path);{
    else
      fController := TMergeController.Create(Path);}
  end;

  destructor TProcessController.Destroy();
  begin
    inherited;
  end;

  //****************************************************************************
  // Methods
  //****************************************************************************
  {
    Toggle
    Pause and continue bound process
  }
  procedure TProcessController.Toggle();
  begin
    fController.Toggle();
  end;

  {
    Cancel
    Cancel bound process
  }
  procedure TProcessController.Cancel();
  begin
    fController.Cancel();
  end;

  //****************************************************************************
  // Properties
  //****************************************************************************
  {
    Get_Max
    Returns max of bound process
  }
  function TProcessController.Get_Max() : Cardinal;
  begin
    Result := fController.Max;
  end;

  {
    Get_Current
    Returns current position of bound process
  }
  function TProcessController.Get_Current() : Cardinal;
  begin
    Result := fController.Current;
  end;

  {
    Get_Running
    sdf
  }
  function TProcessController.Get_Running() : WordBool;
  begin
    Result := fController.Running;
  end;

  {
    Get_Finished
    Returns finish boolean of bound process
  }
  function TProcessController.Get_Finished() : WordBool;
  begin
    Result := fController.Finished;
  end;

  {
    Get_Error
    sdf
  }
  function TProcessController.Get_Error() : WordBool;
  begin
    Result := fController.Error;
  end;

end.
